package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品和口味
     *
     * @param dishDto
     */
    @Override
    public void saveWithFlavor(DishDto dishDto) {

        //调用方法保存菜品
        this.save(dishDto);
        //获取菜品id
        Long dishID = dishDto.getId();

        //获取菜品口味集合  因为没有菜品id 所以需要手动设置
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((dishFlavor) -> {
            dishFlavor.setDishId(dishID);
            return dishFlavor;
        }).collect(Collectors.toList());

        //保存菜品口味
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询
     *
     * @param id
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        Dish dish = this.getById(id);

        //前端需要的是 dishDto对象
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        //根据dish  id查询菜品口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(wrapper);
        dishDto.setFlavors(flavors);

        return dishDto;

    }

    /**
     * 修改菜品
     *
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //1.保存菜品信息
        this.updateById(dishDto);

        //2.清理当前菜品口味
        //获取条件构造器
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        //获取当前菜品id 根据id查询菜品口味
        wrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(wrapper);
        //3.保存菜品口味  需要额外保存菜品id
        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map((dishFlavor)->{
            dishFlavor.setDishId(dishDto.getId());
            return dishFlavor;
        }).collect(Collectors.toList());

        dishDto.setFlavors(flavors);

        //4.保存口味
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    public void deleteWithFlavor(List<Long> ids) {//菜品id
        //判断菜品状态
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(ids!=null,Dish::getId,ids);
        //状态启用的
        wrapper.eq(Dish::getStatus,1);
        int count = this.count(wrapper);

        if(count>0){
            //启用,不能删除
            throw new CustomException("当前菜品正在售卖,不能删除");
        }
        //停用,可以删除
        this.removeByIds(ids);
        //删除口味
        LambdaQueryWrapper <DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(queryWrapper);

    }
}
